package cn.tedu.tmall.admin.mall.controller;

import cn.tedu.tmall.admin.mall.pojo.param.GoodsAddNewParam;
import cn.tedu.tmall.admin.mall.pojo.param.GoodsUpdateInfoParam;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsListItemVO;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsStandardVO;
import cn.tedu.tmall.admin.mall.service.IGoodService;
import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@Api(tags = "2.商品管理")
@RestController
@RequestMapping("/goods")
public class GoodController {

    @Autowired
    private IGoodService service;

    @ApiOperation("发布商品")
    @PreAuthorize("hasAuthority('/mall/goods/simple')")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal,
                             @Valid GoodsAddNewParam goodsAddNewParam){
        service.addNew(principal,goodsAddNewParam);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除商品")
    @PreAuthorize("hasAuthority('/mall/goods/delete')")
    @ApiOperationSupport(order = 110)
    @GetMapping("/{id:[0-9]+}/delete-by-id")
    @ApiImplicitParam(name = "id",value = "商品id",required = true,dataType = "long")
    public JsonResult deleteById(@PathVariable @Range(min = 1,message = "请提供有效的商品id") Long id){
        service.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id修改商品")
    @PreAuthorize("hasAuthority('/mall/goods/simple')")
    @ApiOperationSupport(order = 120)
    @GetMapping("/{id:[0-9]+}/update-by-id")
    @ApiImplicitParam(name = "id",value = "商品id",required = true, dataType = "long")
    public JsonResult updateById(@PathVariable @Range(min = 1,message = "请提供有效的商品id") Long id, GoodsUpdateInfoParam goodsUpdateInfoParam){
        service.updateById(id, goodsUpdateInfoParam);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询商品")
    @PreAuthorize("hasAuthority('/mall/goods/simple')")
    @ApiOperationSupport(order = 130)
    @GetMapping("/{id:[0-9]+}/select-by-id")
    @ApiImplicitParam(name = "id",value = "商品id",required = true, dataType = "long")
    public JsonResult selectById(@PathVariable @Range(min = 1,message = "请提供有效的商品id")Long id){
        GoodsStandardVO goodsStandardVO = service.selectById(id);
        return JsonResult.ok(goodsStandardVO);
    }

    @ApiOperation("查询商品列表")
    @PreAuthorize("hasAuthority('/mall/goods/simple')")
    @ApiOperationSupport(order = 131)
    @GetMapping("/list")
    @ApiImplicitParam(name = "page",value = "页码",required = true,
            defaultValue = "1",paramType = "query", dataType = "int")
    public JsonResult list(@Range(min = 1,message = "请提供有效的页码值") Integer page){
        Integer pageNum = page;
        if(page==null||page<1){
            pageNum=1;
        }
        PageData<GoodsListItemVO> pageData = service.list(pageNum);
        return JsonResult.ok(pageData);
    }

    @ApiOperation("根据类别id查询商品列表")
    @PreAuthorize("hasAuthority('/mall/goods/simple')")
    @ApiOperationSupport(order = 132)
    @GetMapping("/list-by-category-id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId",value = "类别id",required = true,paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,
                    defaultValue = "1",paramType = "query", dataType = "int")
    })
    public JsonResult listByCategoryId(@Range(message = "请提供有效的类别id")Long categoryId,
                                       @Range(min = 1,message = "请提供有效的页码值")Integer page){
        Integer pageNum = page;
        if(page==null||page<1){
            pageNum=1;
        }
        PageData<GoodsListItemVO> pageData = service.listByCategoryId(categoryId, pageNum);
        return JsonResult.ok(pageData);
    }





}
